//数组的深拷贝
let deepcloneArray = arr1 => {
    let arr2 = []
    arr1.forEach((value, index) => {
        if (typeof (value) === 'object') {
            arr2[index] = deepcloneArray(value)
        }
        else {
            arr2[index] = value
        }
    })
    return arr2
}
let arr1 = [1, [2, 3, 4], 5]
let arr2 = deepcloneArray(arr1)
arr1[1][2] = 9
console.log(arr2[1][2]) //4

//如果这里面的数据既有对象，又有数组，这时候的深拷贝
//eg:[1,2,3,{a:1,b:2,c:[4,5,6]}] 或者是 {a:1,b:2,c:[1,2a:1,b:2,c:[1,2,{a:4,b:5}],]}

//判断值的类型是数组还是对象
let type1 = Object.prototype.toString.call({}).slice(8,-1)
console.log(type1)

let type2 = Object.prototype.toString.call([]).slice(8,-1)
console.log(type2)

//如果是对象或者是数组
let checkType =  data =>{
    return Object.prototype.toString.call(data).slice(8,-1)
}

//
let deepClone = target =>{
    let targetType = checkType(target)
    let result
    if(targetType === 'array'){
        result = []
    }
    else if(targetType === 'object'){
        result = {}
    }
    else 
        return target

    Object.keys(target).forEach(key=>{
        let value = target[key]
        let valuetype = checkType(value)
        if(valuetype === 'array' || valuetype === 'object'){
            result[key] = deepClone(value)
        }
        else 
            result[key] = value
    })
    return target
}